-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safe Global Predefined Color Palettes #13598
base: main
Are you sure you want to change the base?
Safe Global Predefined Color Palettes #13598
Conversation
5963bf4
to
a5b7d98
Compare
src/library/basetracktablemodel.cpp
Outdated
@@ -109,7 +109,7 @@ int BaseTrackTableModel::s_bpmColumnPrecision = | |||
kBpmColumnPrecisionDefault; | |||
bool BaseTrackTableModel::s_keyColorsEnabled = kKeyColorsEnabledDefault; | |||
ColorPalette BaseTrackTableModel::s_keyColorPalette = | |||
mixxx::PredefinedColorPalettes::kDefaultKeyColorPalette; | |||
mixxx::predefinedcolorpalettes::get().defaultKeyColorPalette; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider to make PredefinedColorPalettes a singleton? This avoids to initalize it over and over again and will avoid the new predefinedcolorpalettes namespace. |
It is effectively a singleton. It is only getting initialized once. see Static block variables |
This will avoid the "static initialization order fiasco" by putting all palettes behind a C++11 "magic static" that ensure that that they are initialized before they're first used.
a5b7d98
to
1c7afe6
Compare
fyi, an alternative implementation would be to use My current favorite is the latter (gutting out the Qt containers in favor of |
The ColorPalletes are currenrtly copied by value, which is a matter of three pointers due to Qt's implicit sharing. |
@@ -146,7 +146,9 @@ void ColorPaletteEditor::initialize( | |||
m_resetPalette = paletteName; | |||
QString saveName = paletteName; | |||
|
|||
for (const ColorPalette& palette : mixxx::PredefinedColorPalettes::kPalettes) { | |||
const auto& kPalettes = mixxx::predefinedcolorpalettes::get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A free get() function in a namespace looks weird. This is an indicator that the Namespace is an object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, its effectively a singleton. So its up to interpretation whether it should be modeled as a class with a static member function or a namespace with a free function. they're basically equivalent, though I find the namespace more elegant.
kRekordboxHotcueColor16, | ||
}; | ||
|
||
const static PredefinedColorPalettes kPalettes{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning a local static is one way ti implement a singleton pattern. How about fully make use of that here?
as in a PredefinedColorPalettes::instance()
function.
kTritKeyColor11, | ||
kTritKeyColor12, | ||
}}, | ||
.defaultHotcueColorPalette = kPalettes.mixxxHotcueColorPalette, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a deep copy. Can we avoid it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can simply store a const ref because we know the object outlives the reference.
.defaultKeyColorPalette = kPalettes.mixxxKeyColorPalette, | ||
.palettes{ | ||
// Hotcue Color Palettes | ||
kPalettes.mixxxHotcueColorPalette, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This are also deep copies, which should be avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original were also deep copies though IIRC, but it should be easy to change.
summarizing your review. there are two issues:
|
As suggested (by me) in #13594 (comment) as an alternative to that PR.
Note that in case we manage to make
ColorPalette
constexpr friendly, we can trivially migrateget
to be constexpr as well without having to change the callers with this approach.